 $(document).ready(function(){
    // 绑定品类菜单事件
    $('[action=leftCategory]').click(function () {
        if ($(this).hasClass('active')) return;
        $('a.menu.active').removeClass('active');
        $(this).addClass('active');
    });
     yaxis_dataAr = yaxis_datastr.split(',');
   	 var yaxis_data=new Array(yaxis_dataAr.length);
   	 $(yaxis_dataAr).each(function (index, obj) {
   		 yaxis_data[index]=obj+"";
     });
   	 xaxis_dataAr = xaxis_datastr.split(',');
   	 var xaxis_data=new Array(xaxis_dataAr.length);
   	 $(xaxis_dataAr).each(function (index, obj) {
   		 xaxis_data[index]=obj+"";
     });
        var turnover_data = $.map(tmp_data, function(v) {
            return v.trans_amount;
        });
        var turnover_xaxis_data = $.map(tmp_data, function(v) {
            return v.create_time + '@' + v.trans_number;
        });
        
        window.onresize = function(){
            myChart.resize();
         }
         var chartEl = document.getElementById('chart');
         var myChart = echarts.init(chartEl);
        
         var _left = '';
         option = {
             title: {
                 text: '交易行情走势图',
                 x:'center',
                 textStyle:{fontSize:'14px', color:"#479ADD"},
                 textBaseline:'middle'
             },
             grid: [
                 {
                     height: '55%'
                 },
                 {
                     top: '80%',
                     height: '20%'
                 }
             ],
             tooltip: {
                 trigger: 'axis',
                 formatter: function (name) {
                     if ($.isArray(name)) {
                         var _n = name[0].name.replace('false','');
                         var _v = name[0].value;
                         if ( name[0].seriesIndex == 0) {
                             return _n + "<br/>"+catename+"<br/>价格：" + _v;
                         } else if (name[0].seriesIndex == 1) {
                             var _time = _n.substr(0,10);
                             var _transNum = _n.substr(_n.indexOf('@') + 1);
                             return _time + "<br/>"+catename+"<br/>成交量：" + _transNum + "<br/>成交额：" + _v;
                         }
                     }
                 },
                 axisPointer: {animation: false}
             },
             xAxis: [{
                 boundaryGap : false,
                 type: 'category',
                 axisLabel:{
                     textStyle:{color:"#666"},
                     formatter:function (value) {
                         return value.substr(5,6);
                     },
                     interval:function (index, value) {
                         if (value.indexOf('false') > 0) return false;
                         var _arr = value.split(" ");
                         if (_left == _arr[0]) return false;
                         _left = _arr[0];
                         return true;
                     },
                 },
                 splitLine: {show: false},
                 data:xaxis_data,
             },{
                 boundaryGap : false,
                 type: 'category',
                 gridIndex: 1,
                 splitLine: {show: false},
                 axisLabel: {show: false},
                 data:turnover_xaxis_data,
             }],
             yAxis: [{
                 type: 'value',
                 scale: true,
                 splitNumber:8,
                 boundaryGap: [0, '100%'],
                 splitLine: {show: false},
                 axisLabel:{
                     textStyle:{color:"#667"},
                     formatter:function (value, index) {
                         if (value>999) return value / 1000 + "K";
                         return value;
                     }
                 }
             },{
                 scale: true,
                 gridIndex: 1,
                 splitNumber: 2,
                 axisLabel: {show: false},
                 axisLine: {show: false},
                 axisTick: {show: false},
                 splitLine: {show: false}
             }],
             series: [
             {
 	            name:'最高价',
 	            type:'line',
 	            data:yaxis_data,
 	            smooth : true,
 	            markPoint: {
 	                data: [
 	                    {type: 'max',name:"最高价"},
 	                    {type: 'min',name:"最低价"}
 	                ]
 	            },
 	            markLine: {
 	                data: [
 	                    {type: 'average',name:"平均价"}
 	                ]
 	            }
 	        },{
                name: '成交量',
                type: 'bar',
                xAxisIndex: 1,
                yAxisIndex: 1,
                itemStyle:{normal:{color:'#FF6524'}},
                data: turnover_data,
            }]
         };
         if (yaxis_data && yaxis_data.length > 0) {
             myChart.setOption(option, true);
         }
        
        $('[action=confirm_buying]').click(function () {
        	if($("#buy_type").val()=='2' && $("#buy_type_period").val()==''){
        		showModal('请选择贷款产品');
        		return false;
        	}
        	
        	if($("#buy_type").val()=='2' && $("#diya-term_period").val()=='') {
        		showModal('请选择贷款周期');
        		return false;
        	}
        	var credit=$("#buy_type_period").find("option:selected").attr("data-id");
        	if($("#buy_type").val()=='2' && credit==null) {
        		showModal('请选择贷款产品');
        		return false;
        	}
        	if($("#buy_type").val()=='2' && parseInt(credit)>parseInt($("#buy_price").val())) {
        		showModal('买入单价必须大于授信额度￥'+credit);
        		return false;
        	}
        	if($("#buy_type").val()=='2' && $("#buy_type_period").val()==null) {
        		showModal('请选择贷款产品');
        		return false;
        	}
        	if(isNaN($("#buy_price").val()) || $("#buy_price").val()<=0) {
        		showModal('买入单价必须为大于0数字');
        		return false;
        	}
        	if(isNaN($("#buy_number").val()) || $("#buy_number").val()<=0) {
        		showModal('买入数量必须为大于0数字');
        		return false;
        	}
        	if($.trim($("#bypaswd").val()).length==0) {
        		showModal('请输入交易密码');
        		return false;
        	}
            showConfirm({
                msg:"您确认以单价"+$("#buy_price").val()+"元买入"+$("#buy_number").val()+"个"+$(".ddname").text(),
                confirmed:function(){
                    isCancelling = true;
                    $.ajax({
                		type: "POST", 
                		url:$("#frm_buying").attr("action"), 
                		data:$('#frm_buying').serialize(),// 你的formid 
                		async: false, 
                		error: function(request) { showModal("system error");}, 
                		success: function(data){
                			showModal(data.info);
                		}
                	}).fail(function(){
                        isCancelling = false;
                    });
                }
            });
        });
        $('[action=closeModal]').click(function () {
        	$('[popup=confirm]').css("display","none");
        	$('.reveal-modal-bg').css("display","none");
        });
        $('[action=cancel]').click(function () {
        	$('[popup=confirm]').css("display","none");
        	$('.reveal-modal-bg').css("display","none");
        });
        
        $(".menu").mouseover(function(){
        	$(".menu").removeClass("mousactive");
        	$(this).addClass("mousactive");
        });
        $(".menu").mouseout(function(){
        	$(".menu").removeClass("mousactive");
        });
        
        $('[action=confirm_selling]').click(function () {
        	if($("#sell_number").val()=='2' && $("#sell_number").val()==''){
        		showModal('请填写卖出数量');
        		return false;
        	}
        	if(isNaN($("#sell_price").val()) || $("#sell_price").val()<=0) {
        		showModal('卖出单价必须为大于0数字');
        		return false;
        	}
        	if($.trim($("#sellpassid").val()).length==0) {
        		showModal('请输入交易密码');
        		return false;
        	}
        	showConfirm({
                msg:"您确认以单价"+$("#sell_price").val()+"元卖出"+$("#sell_number").val()+"个"+$(".ddname").text(),
                confirmed:function(){
                    isCancelling = true;
                    $.ajax({
                		type: "POST", 
                		url:$("#frm_sellingid").attr("action"), 
                		data:$('#frm_sellingid').serialize(),// 你的formid 
                		async: false, 
                		error: function(request) { showModal("system error");}, 
                		success: function(data){
                			showModal(data.info);
                		}
                	}).fail(function(){
                        isCancelling = false;
                    });
                }
            });
        });
    });
 $("#buy_price").blur(function(){
	 var credit=$("#buy_type_period").find("option:selected").attr("data-id");
	 if(isNaN($(this).val())  || $.trim($(this).val()).length==0){
		 $("#buy_total").html("<span class='red06 f12'>请输入正确格式的金额</span>");
	 }
     else if(parseInt($(this).val())<parseInt(credit) && $("#buy_type").val()=='2') {
		 $("#buy_total").text("信用购买单价不能低于"+credit+"元");
	 } else if(!isNaN($("#buy_number").val()) && $.trim($("#buy_number").val())!=''){
		 var cha = 0;
		 if($("#buy_type").val()=='2') {
			 cha = (parseInt($(this).val())-parseInt(credit))*parseInt($("#buy_number").val());
		 } else{
			 cha = parseInt($(this).val())*parseInt($("#buy_number").val());
		 }
		 $("#buy_total").html(cha+"("+upDigit(cha)+") <span class='red06 f12'>已扣除授信额度</span>");
	 }
 });
 $("#buy_number").blur(function(){
	 var credit=$("#buy_type_period").find("option:selected").attr("data-id");
	 if(isNaN($(this).val())  || $.trim($(this).val()).length==0){
		 $("#buy_total").html("<span class='red06 f12'>请输入正确格式的买入数量</span>");
	 } else if(!isNaN($("#buy_price").val()) && $.trim($("#buy_price").val())!=''){
		 var cha = 0;
		  if(parseInt($("#buy_price").val())<parseInt(credit) && $("#buy_type").val()=='2') {
			 $("#buy_total").text("信用购买单价不能低于"+credit+"元");
		 }
		  else if($("#buy_type").val()=='2') {
			 cha =(parseInt($("#buy_price").val())-parseInt(credit))*parseInt($(this).val());
		 }
		 else{
			 cha = parseInt($("#buy_price").val())*parseInt($(this).val());
		 }
		 $("#buy_total").html(cha+"("+upDigit(cha)+") <span class='red06 f12'>已扣除授信额度</span>");
	 }
 });
 
 $("#sell_number").blur(function(){
	 if(isNaN($(this).val()) || $.trim($(this).val()).length==0){
		 $("#sell_total").html("<span class='red06 f12'>请输入正确格式数量</span>");
	 }
     else if(!isNaN($("#sell_price").val()) && $.trim($("#sell_price").val()).length>0){
		 var cha = parseInt($(this).val())*parseInt($("#sell_price").val());
		 $("#sell_total").html(cha+"("+upDigit(cha)+") <span class='red06 f12' id='commission_result'>成交手续费(0.3%)</span>");
	 }
 });
 $("#sell_price").blur(function(){
	 if(isNaN($(this).val()) || $.trim($(this).val()).length==0){
		 $("#sell_total").html("<span class='red06 f12'>请输入正确格式金额</span>");
	 } else if(!isNaN($("#sell_number").val()) && $.trim($("#sell_number").val())!=''){
		 var cha = parseInt($(this).val())*parseInt($("#sell_number").val());
		 $("#sell_total").html(cha+"("+upDigit(cha)+") <span class='red06 f12' id='commission_result'>成交手续费(0.3%)</span>");
	 }
 });
 function upDigit(n)   
 {  
     var fraction = ['角', '分'];  
     var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];  
     var unit = [ ['元', '万', '亿'], ['', '拾', '佰', '仟']  ];  
     var head = n < 0? '欠': '';  
     n = Math.abs(n);  
     var s = '';  
     for (var i = 0; i < fraction.length; i++)   
     {  
         s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');  
     }  
     s = s || '整';  
     n = Math.floor(n);  
     for (var i = 0; i < unit[0].length && n > 0; i++)   
     {  
         var p = '';  
         for (var j = 0; j < unit[1].length && n > 0; j++)   
         {  
             p = digit[n % 10] + unit[1][j] + p;  
             n = Math.floor(n / 10);  
         }  
         s = p.replace(/(零.)*零$/, '').replace(/^$/, '零')  + unit[0][i] + s;  
     }  
     return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');  
 }
 